package testmybatis.springmybatis;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Map;

/**
 * @Description 模拟MapperScannerRegistrar
 * @Author dangxianyue
 * @Date 2024/3/27 21:40
 */
public class MyMapperScannerRegistrarV2 implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(final AnnotationMetadata importingClassMetadata, final BeanDefinitionRegistry registry, final BeanNameGenerator importBeanNameGenerator) {
        final Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(MyMapperScan.class.getName());
        String path = (String) annotationAttributes.get("value");
        System.out.println("path: " + path); // testmybatis.mapper

        final MyMapperScanner myMapperScanner = new MyMapperScanner(registry);
        myMapperScanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(final MetadataReader metadataReader, final MetadataReaderFactory metadataReaderFactory) throws IOException {
                return true;
            }
        });

        myMapperScanner.scan(path);
    }
}
